

library(cjoint)
library(tidyverse)


conjoint_data <- read.qualtrics("Israel_2024_survey.csv",responses= NULL, covariates= c("Q4", "Q11_1"), respondentID="cintID", ranks=c("Q23_1", "Q24_1", "Q25_1" , "Q26_1", "Q27_1", "Q23_2", "Q24_2", "Q25_2" , "Q26_2", "Q27_2"))



## Translate Hebrew into English
Israeldemoc <- conjoint_data %>% 
  select(!c(contains("rowpos")))

Israeldemoc <- Israeldemoc %>%
  mutate(
    Democraticrules= case_when(
      Adherence.to.the.democratic.rules == "בעד"~"Support.d",
      Adherence.to.the.democratic.rules== "נגד"~"Oppose.d"
    )
  )

Israeldemoc <- Israeldemoc %>%
  mutate(
    Authoritarian = case_when(
      authoritarian.leadership ==  "בעד"~"Support.a",
      authoritarian.leadership ==  "נגד"~"Oppose.a"
    )
  )

Israeldemoc <- Israeldemoc %>%
  mutate(
    Commitment = case_when(
      commitment ==  "בעד"~"Support.c",
      commitment ==  "נגד"~"Oppose.c"
    )
  )
Israeldemoc <- Israeldemoc %>%
  mutate(
    Equalrights = case_when(
      equal.rights ==  "בעד"~"Support.e",
      equal.rights ==  "נגד"~"Oppose.e"
    )
  )
Israeldemoc <- Israeldemoc %>%
  mutate(
    FreedomOfSpeech = case_when(
      freedom.of.speech == "בעד"~"Support.f",
      freedom.of.speech ==  "נגד"~"Oppose.f"
    )
  )
Israeldemoc <- Israeldemoc %>%
  mutate(
    JudicialIndependent = case_when(
      judicial.independent ==  "בעד"~"Support.j",
      judicial.independent ==  "נגד"~"Oppose.j"
    )
  )
Israeldemoc <- Israeldemoc %>%
  mutate(
    tGovernment = case_when(
      trust.in.gov ==  "אמון  מועט"~"Trust very much.g",
      trust.in.gov == "אמון רב"~"Trust.g",
      trust.in.gov == "אמון רב  מאד"~"Not trust.g",
      trust.in.gov == "כלל אין  אמון"~"Never trust.g"
    )
  )
Israeldemoc <- Israeldemoc %>%
  mutate(
    tKnesset = case_when(
      trust.in.knesset ==  "אמון  מועט"~"Trust very much.k",
      trust.in.knesset == "אמון רב"~"Trust.k",
      trust.in.knesset == "אמון רב  מאד"~"Not trust.k",
      trust.in.knesset == "כלל אין  אמון"~"Never trust.k"
    )
  )

Israeldemoc <- Israeldemoc %>%
  mutate(
    tSupremeCourt = case_when(
      trust.in.supreme.court == "אמון  מועט"~"Trust very much.s",
      trust.in.supreme.court == "אמון רב"~"Trust.s",
      trust.in.supreme.court == "אמון רב  מאד"~"Not trust.s",
      trust.in.supreme.court == "כלל אין  אמון"~"Never trust.s"
    )
  )




## Combine Attribute Levels
Israeldemoc <- Israeldemoc |>
  mutate(tGovernment = case_when(
    tGovernment %in% c("Not trust.g", "Never trust.g") ~ "Not trust.g",
    tGovernment %in% c("Trust.g", "Trust very much.g") ~ "Trust.g",
    TRUE ~ tGovernment 
  ))

Israeldemoc <- Israeldemoc |>
  mutate(tKnesset = case_when(
    tKnesset %in% c("Not trust.k", "Never trust.k") ~ "Not trust.k",
    tKnesset %in% c("Trust.k", "Trust very much.k") ~ "Trust.k",
    TRUE ~ tKnesset
  ))

Israeldemoc <- Israeldemoc |>
  mutate(tSupremeCourt = case_when(
    tSupremeCourt %in% c("Not trust.s", "Never trust.s") ~ "Not trust.s",
    tSupremeCourt %in% c("Trust.s", "Trust very much.s") ~ "Trust.s",
    TRUE ~ tSupremeCourt
  ))




## Convert into a Factor
Israeldemoc$Democraticrules <- as.factor(Israeldemoc$Democraticrules)
Israeldemoc$Authoritarian <- as.factor(Israeldemoc$Authoritarian)
Israeldemoc$Commitment <- as.factor(Israeldemoc$Commitment)
Israeldemoc$Equalrights <- as.factor(Israeldemoc$Equalrights)
Israeldemoc$FreedomOfSpeech <- as.factor(Israeldemoc$FreedomOfSpeech)
Israeldemoc$JudicialIndependent <- as.factor(Israeldemoc$JudicialIndependent)
Israeldemoc$tGovernment <- as.factor(Israeldemoc$tGovernment)
Israeldemoc$tKnesset <- as.factor(Israeldemoc$tKnesset)
Israeldemoc$tSupremeCourt <- as.factor(Israeldemoc$tSupremeCourt)


Israeldemoc <- na.omit(Israeldemoc)




## Assign Labels
attr_list <- list()             
attr_list[["Democraticrules"]] <- c("Support.d","Oppose.d")

attr_list[["Authoritarian"]] <- c("Support.a","Oppose.a")

attr_list[["Commitment"]] <- c("Support.c","Oppose.c")

attr_list[["Equalrights"]] <- c("Support.e","Oppose.e") 

attr_list[["FreedomOfSpeech"]] <- c("Support.f","Oppose.f")

attr_list[["JudicialIndependent"]] <- c("Support.j","Oppose.j")

attr_list[["tGovernment"]] <- c( "Not trust.g", "Trust.g")

attr_list[["tKnesset"]] <- c("Not trust.k", "Trust.k")

attr_list[["tSupremeCourt"]] <- c("Not trust.s", "Trust.s")




## Conjoint Analysis
cjoint_design <- makeDesign(type = "constraints",
                            attribute.levels = attr_list)


cjoint_pool <- cjoint::amce(selected ~ Democraticrules  + Authoritarian + Commitment + Equalrights + FreedomOfSpeech + JudicialIndependent + tGovernment + tKnesset + tSupremeCourt, 
                    data=Israeldemoc,
                    respondent.id= "respondentIndex",
                    cluster = F,
                    na.ignore=TRUE)




library(extrafont)

plot(cjoint_pool, point.size = 1.2, dodge.size = 1.2, text.size = 18)

summary(cjoint_pool)


